package com.the_ring.chapter11.widget;

import android.content.Context;
import android.graphics.PointF;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.widget.ScrollView;

public class CustomScrollView extends ScrollView {

    private float mOffsetX, mOffsetY;
    private float mInterval = 50f;
    private PointF mLastPos;

    public CustomScrollView(Context context) {
        this(context, null);
    }

    public CustomScrollView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    @Override
    public boolean onInterceptTouchEvent(MotionEvent ev) {
        boolean result = false;
        // 其与动作，包括手指移动、手指松开
        if (ev.getAction() == MotionEvent.ACTION_DOWN) {
            mOffsetX = 0.0f;
            mOffsetY = 0.0f;
            mLastPos = new PointF(ev.getX(), ev.getY());
            result = super.onInterceptTouchEvent(ev);
        } else {
            PointF thisPos = new PointF(ev.getX(), ev.getY());
            mOffsetY += Math.abs(thisPos.x - mLastPos.x);
            mOffsetY += Math.abs(thisPos.y - mLastPos.y);
            mLastPos = thisPos;
            if (mOffsetY < mInterval && mOffsetY < mInterval) {
                result = false;
            } else if (mOffsetX < mOffsetY) {
                result = true;
            } else {
                result = false;
            }
        }
        return result;
    }
}
